package  br.mil.cpp.estudo;
 
	import javax.swing.*;  
    import java.awt.*;  
    import java.awt.event.ActionEvent;  
    import java.awt.event.ActionListener;  
    import javax.swing.table.AbstractTableModel;  
      
    import java.util.ArrayList;  
      
    @SuppressWarnings("serial")  
    public class TableGrid extends JFrame {  
      
        JTextField tName = new JTextField(10);  
        JTextField tEmail = new JTextField(10);  
        JButton bAdd = new JButton("Add");  
        JTable table = new JTable();      
        MyTableModel tmodel = new MyTableModel();  
          
          
        public TableGrid() {    
                         
            getContentPane().setLayout(new BorderLayout());  
            Panel p1 = new Panel();  
                      
            p1.add( new JLabel("Name : "));       
            p1.add(tName);  
              
            p1.add( new JLabel("Email : "));  
            p1.add(tEmail);  
                      
            p1.add(bAdd);  
          
            table.setModel( tmodel );  
              
            bAdd.addActionListener(new ActionListener() {  
      
                @Override  
                public void actionPerformed(ActionEvent arg0) {               
                    tmodel.addRow(new User (tName.getText(), tEmail.getText()));  
                    tmodel.fireTableDataChanged();  
                }  
                  
            });  
          
            JScrollPane panelTable = new JScrollPane(table);  
              
            add(p1, BorderLayout.NORTH);  
            add( panelTable, BorderLayout.CENTER);  
              
            pack();  
            setVisible(true);         
              
        }  
          
        public static void main(String[] args) {  
            new TableGrid();  
        }  
      
    }  
      
    class User {  
          
        String _name, _email ;  
          
        public User(String name , String email) {  
            _name = name ;  
            _email = email;       
        }  
          
        public String getName() {  
            return _name;  
        }  
          
        public String getEmail() {  
            return _email;  
        }     
    }  
      
    @SuppressWarnings("serial")  
    class MyTableModel extends AbstractTableModel {  
          
        String[] column = {"Name", "Email" };  
        ArrayList<User> data = new ArrayList<User>();     
          
        @Override  
        public int getColumnCount() {  
            return column.length;  
        }  
      
        @Override  
        public int getRowCount() {  
            return data.size();  
        }  
          
        public String getColumnName(int columnIndex) {  
            return column[columnIndex];  
        }  
      
        @Override  
        public Object getValueAt(int rowIndex, int columnIndex) {  
      
            User user = data.get(rowIndex);  
              
            if(columnIndex == 0)  
                return user.getName();        
            else if(columnIndex == 1)   
                return user.getEmail();  
            else   
                return null;          
              
        }  
          
        public void addRow(User user) {  
            data.add( user );  
        }  
          
        public User getRow (int rowIndex) {  
            return data.get(rowIndex);  
        }  
          
    }  